package com.xique.door.controller;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.DCard;
import com.xique.door.bean.request.card.DeviceAddCardRequest;
import com.xique.door.bean.request.card.DeviceCardListRequest;
import com.xique.door.bean.request.card.DeviceCardSelectListRequest;
import com.xique.door.bean.request.deviceUser.DeviceDeleteUserRequest;
import com.xique.door.service.ICardAuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 卡授权
 */
@RestController
@RequestMapping("/card")
public class CardAuthorizationController extends BaseController {

    @Autowired
    private ICardAuthorizationService cardAuthorizationService;

    /**
     * 卡片信息
     */
    @GetMapping("/CardInfo/list")
    public TableDataInfo CardInfoCardList(DeviceCardSelectListRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.selectDCardAdministration(request);
    }

    /**
     * 设备已授权的卡片
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @GetMapping("/deviceCard/list")
    public TableDataInfo AssignedDeviceCardList(DeviceCardSelectListRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.selectAssignedDeviceCardList(request);
    }

    /**
     * 设备未授权的卡片
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @GetMapping("/deviceCardUnAssigned/list")
    public TableDataInfo UnAssignedDeviceCardList(DeviceCardSelectListRequest request) {
        List<DCard> ids =
                cardAuthorizationService.selectDCardDeviceList(request.getParkId(), request.getDeviceId());
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.selectUnAssignedDeviceCardList(request, ids);
    }

    /**
     * 卡片重新下发
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @PostMapping("/addCard")
    public AjaxResult addDeviceCard(@RequestBody DeviceAddCardRequest request) {
        if (request.getDeviceId() == null
                || request.getUserId() == null && request.getCardId() == null) {
            return AjaxResult.error("请检查是否勾选用户和设备");
        }
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.addDeviceCard(request);
    }

    /**
     * 卡片解绑
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @PostMapping("/deleteCard")
    public AjaxResult daleteDeviceCard(@RequestBody DeviceAddCardRequest request) {
        if (request.getDeviceId() == null
                || request.getUserId() == null && request.getCardId() == null) {
            return AjaxResult.error("请检查是否勾选用户和设备");
        }
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.deleteDeviceCard(request);
    }

    /**
     * 批量绑卡
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @PostMapping("/addCardList")
    public AjaxResult addDeviceCardList(@RequestBody DeviceCardListRequest request) {
        if (request.getDeviceId() == null || request.getList() == null) {
            return AjaxResult.error("请检查是否勾选卡片和设备");
        }
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.addDeviceCardList(request);
    }

    /**
     * 卡片批量重新下发
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @PostMapping("/batchAddCardList")
    public AjaxResult addDeviceCardLists(@RequestBody DeviceCardListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.addDeviceCardLists(request);
    }

    /**
     * 卡片全量同步
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @PostMapping("/addAllCardList")
    public AjaxResult addAllDeviceCardLists(@RequestBody DeviceCardListRequest request) {
        if (request.getDeviceId() == null) {
            return AjaxResult.error("必填项不能为空");
        }
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.addAllDeviceCardLists(request);
    }

    /**
     * 卡片批量解绑
     */
    @PreAuthorize(hasPermi = "door:cardAuthorization:list")
    @PostMapping("/batchDeleteCardList")
    public AjaxResult daleteDeviceCardList(@RequestBody DeviceCardListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return cardAuthorizationService.deleteDeviceCardList(request);
    }
}
